package com.lzx.iteam;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.lzx.iteam.R;
import com.lzx.iteam.net.*;
import com.lzx.iteam.util.AllDialogUtil;
import com.lzx.iteam.util.Constants;
import com.lzx.iteam.util.StringUtil;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hfx on 15-1-25.
 */
public class ResetPswActivity extends Activity implements View.OnClickListener {
    private Context mContext;
    private EditText mEtOldPsw,mEtNewPsw;
    private Button mBtnBack,mBtnConfirm;
    private LinearLayout mBack;

    private AsynHttpClient mAsynClient;
    private Dialog mProgressDialog;
    private AllDialogUtil dialogUtil;

    public static final int MSG_MODIFY_PASSWD = 2006;
    public static final int PASSWORD_ERROR = -1069023211;

    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            int what = msg.what;
            switch (what){
                case MSG_MODIFY_PASSWD:
                    if (msg.arg1 == 0){
                        dialogUtil.titleMsgBtnStyle("提示", getString(R.string.modify_passwd_success), getString(R.string.ok));
                        dialogUtil.setOnDialogListener(new AllDialogUtil.DialogOnClickListener() {

                            @Override
                            public void onConfirmClick(Dialog lDialog) {
                                lDialog.dismiss();
                            }
                            @Override
                            public void onCancelClick(Dialog lDialog) {
                            }
                        });

                    }else{
                        String str = "修改失败";
                        if (msg.arg1 == AsynHttpClient.ERROR_LOCAL) {

                            str = getString(R.string.active_failure_no_network);
                        } else {

                            str = (String)msg.obj;
                        }

                        dialogUtil.titleMsgBtnStyle("修改失败", str, getString(R.string.ok));
                        dialogUtil.setOnDialogListener(new AllDialogUtil.DialogOnClickListener() {

                            @Override
                            public void onConfirmClick(Dialog lDialog) {
                                lDialog.dismiss();
                            }

                            @Override
                            public void onCancelClick(Dialog lDialog) {
                            }
                        });
                    }
                    httpReqDlgDismiss();

                break;
            }
        }
    };

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.reset_psw_layout);
        mContext = this;
        mAsynClient = AsynHttpClient.getInstance(mContext);
        dialogUtil = AllDialogUtil.getInstance(this);
        initView();
    }

    private void initView() {
        mEtOldPsw = (EditText) findViewById(R.id.et_old_psw);
        mEtNewPsw = (EditText) findViewById(R.id.et_new_psw);
        mBack = (LinearLayout) findViewById(R.id.reset_ll_back);
        mBack.setOnClickListener(this);
        mBtnConfirm = (Button) findViewById(R.id.btn_resetpsw_confirm);
        mBtnConfirm.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.reset_ll_back:
                ResetPswActivity.this.finish();
                break;
            case R.id.btn_resetpsw_confirm:
                String oldPsw = mEtOldPsw.getText().toString();
                String newPsw = mEtNewPsw.getText().toString();
                if(!StringUtil.isEmpty(oldPsw) && !StringUtil.isEmpty(newPsw) && !newPsw.equals(oldPsw)){
                    //检查原始密码是否正确
                    //重置密码
                    modifyPasswd(oldPsw, newPsw);

                }else{
                    if(StringUtil.isEmpty(oldPsw)){
                        Toast.makeText(mContext, "原始密码不能为空", Toast.LENGTH_SHORT).show();
                    }else if(StringUtil.isEmpty(newPsw) || newPsw.length()<6){
                        Toast.makeText(mContext,"新密码不能为空",Toast.LENGTH_SHORT).show();
                    }else if (newPsw.equals(oldPsw)){
                        Toast.makeText(mContext,"修改失败，新密码和原密码一致",Toast.LENGTH_SHORT).show();
                    }
                }
                break;
            default:
                break;
        }
    }

    /**
     * 修改密码
     * @param oldPasswd
     * @param newPasswd
     */
    private void modifyPasswd(String oldPasswd, String newPasswd) {

        List<NameValuePair> mList = new ArrayList<NameValuePair>();
        mList.add(new BasicNameValuePair(AsynHttpClient.KEY_SID, LocalLogin.getInstance().mSid));
        mList.add(new BasicNameValuePair(AsynHttpClient.KEY_OLD_PASSWORD, oldPasswd));
        mList.add(new BasicNameValuePair(AsynHttpClient.KEY_NEW_PASSWORD, newPasswd));

        httpReqDlgShow();
        SetPasswdMessage setpasswdMsg = new SetPasswdMessage(
                mHandler.obtainMessage(MSG_MODIFY_PASSWD));
        setpasswdMsg.mParams = mList;
        setpasswdMsg.mApi = AsynHttpClient.API_USER_RESETPASS;
        mAsynClient.execUserHttp(setpasswdMsg);
    }


     class SetPasswdMessage extends CcMsgStructure {

        private Message mmCallback;

        public SetPasswdMessage(Message callback) {
            mmCallback = callback;
        }

        @Override
        public void onReceive(JSONObject data) {
            if (mmCallback != null)
                mmCallback.sendToTarget();
        }

        @Override
        public void onError(int error, Object ret) {

            String errorMsg = null;
            if (ret != null) {
                JSONObject jData = null;
                try {
                    jData = new JSONObject(ret.toString());
                    errorMsg = jData.getString("msg");
                } catch (JSONException e) {
                    errorMsg = "服务端数据解析失败!";
                    e.printStackTrace();
                }

            }
            if(mmCallback != null){
                mmCallback.arg1 = error;
                if(!StringUtil.isEmpty(errorMsg)){
                    mmCallback.obj = errorMsg;
                }else{
                    mmCallback.obj = Constants.NETWORK_WRROR;
                }
                mmCallback.sendToTarget();
            }
        }
    }

    private void httpReqDlgShow() {
        if (mProgressDialog == null) {
            mProgressDialog = AllDialogUtil.getInstance(this).styleProgress("请稍后...");
            mProgressDialog.setCancelable(false);
        }
        mProgressDialog.show();

    }

    private void httpReqDlgDismiss() {
        if (mProgressDialog != null && mProgressDialog.isShowing()) {
            mProgressDialog.dismiss();
            mProgressDialog = null;
        }
    }

}